<?php

class Drivers extends CActiveRecord
{	//public $name; 
	/**
	 * The followings are the available columns in table 'Drivers':
	 * @var integer $id
	 * @var string $name
	 * @var string $short_name
	 * @var string $household_register
	 * @var integer $sex
	 * @var string $birthday
	 * @var string $card_id
	 * @var string $files_number
	 * @var string $driver_number
	 * @var string $car_type
	 * @var string $dl_approve_date
	 * @var string $di_valid_date
	 * @var string $dl_Annual_date
	 * @var string $home_tel
	 * @var string $home_addr
	 * @var string $entry_time
	 * @var string $mobile_no
	 * @var string $mobile_nopic_path
	 * @var string $incentive
	 * @var integer $state
	 * @var string $create_time
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Drivers';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('name','length','max'=>7),
			array('short_name','length','max'=>15),
			array('household_register','length','max'=>8),
			array('card_id','length','max'=>18),
			array('archive_id','length','max'=>18),
			array('dl_id','length','max'=>18),
			array('car_type','length','max'=>10),
			array('home_tel','length','max'=>12),
			array('home_addr','length','max'=>30),
			array('mobile_no','length','max'=>11),
			array('name, short_name, household_register, birthday, card_id, archive_id, dl_id, car_type, dl_approve_date, di_valid_date, dl_Annual_date, entry_time, sex, state, create_time', 'required'),
			array('sex', 'numerical', 'integerOnly'=>true),
			array('birthday,dl_approve_date,di_valid_date,dl_Annual_date,entry_time', 'type','type'=>'date'),
		);
	}
	
	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)//在验证之前将无需用户输入的字段写入
	{
		if($this->isNewRecord){
			$this->state=0;
			$this->create_time=date('Y-m-d G:i:s');
		}
		if($this->name!=null&&$this->name!='') $this->short_name=ChineseSpell::getFullSpell($this->name); 
		return true;
	}
	
	protected function afterValidate($on){
		if($this->hasErrors()||!Yii::app()->getRequest()->isPostRequest) return;//已有错误信息即退出
		$tmpPath=Yii::app()->params['uploadFile']['tmpPath'];
		$photoPath=Yii::app()->params['uploadFile']['driverPhotoPath'];
		$basePath=Yii::app()->basePath.'/..';
		$this->del_tmp_photo($basePath.$tmpPath);//删除前天以前的临时目录里的文件
		
		if($this->isNewRecord&&$this->pic_path!=''){//添加记录，并且上传了照片
			$newPath=str_ireplace($tmpPath,$photoPath,$this->pic_path);//建立照片存储新路径 tmp > driverPhoto
			if(is_writable($basePath.$this->pic_path)){
				rename($basePath.$this->pic_path,$basePath.$newPath);
				$this->pic_path=$newPath;
			}
			else{//移动文件失败
				$this->pic_path='';
			}				
		}
		else if(!$this->isNewRecord){//修改记录
			$old_path=$this->findByPk($this->id)->pic_path;
			if(strcasecmp($this->pic_path,$tmpPath)>0){//图片添加或修改
				$newPath=str_ireplace($tmpPath,$photoPath,$this->pic_path);//建立照片存储新路径 tmp > driverPhoto
				if($old_path!='')//修改照片 将原有照片删除
					if(is_writable($basePath.$old_path)) unlink($basePath.$old_path);//删除原有照片文件
				if(is_writable($basePath.$this->pic_path)){
					rename($basePath.$this->pic_path,$basePath.$newPath);//移动文件 tmp > drivePhoto
					$this->pic_path=$newPath;
				}
				else//移动文件失败
					$this->pic_path='';
			}
			else if($this->pic_path==''&&$old_path!=''){//图片删除
				if(is_writable($basePath.$old_path)) unlink($basePath.$old_path);//删除原有照片文件
			}
		}
	}
	
	public function del_tmp_photo($path){//删除前天以前的临时目录里的文件
		$dir=opendir($path);
		while (($file = readdir($dir)) != false) {
		 	if(strlen($file)>12&&(substr($file,0,8)-date('Ymd')<-1)) 
				if(is_writable($path.'/'.$file)) unlink($path.'/'.$file);
    	}
		closedir($dir);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '索引号',
			'name' => '姓名',
			'short_name' => '姓名拼音缩写',
			'household_register' => '户籍',
			'sex' => '性别',
			'birthday' => '出生日期',
			'card_id' => '身份证号',
			'archive_id' => '文件档案编号',
			'dl_id' => '驾驶证号',
			'car_type' => '准驾车型',
			'dl_approve_date' => '初领证日期',
			'di_valid_date' => '驾驶证有效期至',
			'dl_Annual_date' => '驾驶证年审',
			'home_tel' => '住宅电话',
			'home_addr' => '家庭地址',
			'entry_time' => '入职时间',
			'mobile_no' => '手机',
			'pic_path'=>'照片',
			'incentive' => '奖惩记录',
			'state' => '状态',
			'create_time' => '创建日期',
		);
	}
	
	public function safeAttributes(){
		return parent::safeAttributes();
	}
}